package logic

import (
	"context"
	"errors"
	"jd_srv/internal/svc"
	"jd_srv/jd"
	"jd_srv/models"

	"github.com/zeromicro/go-zero/core/logx"
)

type ListGoodsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewListGoodsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListGoodsLogic {
	return &ListGoodsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ListGoodsLogic) ListGoods(in *jd.ListGoodsRequest) (*jd.ListGoodsResponse, error) {
	// todo: add your logic here and delete this line

	var g models.Goods
	var listGoods []*jd.GoodsList

	find, err := g.GetGoods(int(in.Page), int(in.PageSize))
	if err != nil {
		logx.Error("获取商品信息失败")
		return nil, errors.New("获取商品信息失败")
	}

	for _, gs := range find {
		list := jd.GoodsList{
			Id:      int64(gs.ID),
			Title:   gs.Title,
			TypesId: gs.TypesId,
			Price:   float32(gs.Price),
			Stock:   gs.Stock,
			Status:  gs.GoodsStatus,
			AuditId: gs.StatusId,
		}
		listGoods = append(listGoods, &list)
	}

	return &jd.ListGoodsResponse{
		List: listGoods,
	}, nil
}
